package com.ruoyi.web.controller.admin;

import com.ruoyi.admin.utils.qinniu.QiniuService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author Shenjh
 * @version 1.0.0
 * @title imageUploadController
 * @description 图片上传
 * @date 2023/2/6 11:03
 */
@Api(value = "图片上传 Controller", tags = {"图片上传"})
@RestController
@RequestMapping("/api/image")
public class imageUploadController {

    @Resource
    private QiniuService qiniuService;

    /**
     * 自定义图片上传
     */
    @PostMapping("/upload")
    public AjaxResult avatar(@RequestParam("file") MultipartFile file) throws Exception {
        if (!file.isEmpty()) {
            try {
                String fileUrl=qiniuService.saveImage(file);
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", fileUrl);
                return ajax;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }
}
